package com.ews.onlineexchange.controller;

import java.util.Date;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.FileUploadEvent;

import com.ews.onlineexchange.enumType.RecordStatus;
import com.ews.onlineexchange.model.Attachment;
import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.DeterminedRecord;
import com.ews.onlineexchange.model.DeterminedRecordAuditHistory;
import com.ews.onlineexchange.model.RejectRecord;
import com.ews.onlineexchange.model.User;
import com.ews.onlineexchange.service.EntityHome;
import com.ews.onlineexchange.service.RejectRecordHome;
import com.ews.onlineexchange.service.SmsService;
import com.ews.onlineexchange.service.DeterminedRecordService;
import com.ews.onlineexchange.service.UserService;
import com.ews.onlineexchange.util.AttachmentUtils;

@Named
@ConversationScoped
public class DeterminedRecordrHome extends EntityHome<DeterminedRecord, String> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	@Inject
	UserService userService;

	@Override
	public String saveAndFlush() {
		if(getInstance().getAttachments().size() == 0) {
			FacesContext.getCurrentInstance().addMessage("attachments",new FacesMessage(FacesMessage.SEVERITY_ERROR, "请至少上传一个整改情况材料！", ""));
			return null;
		}
	
		return super.saveAndFlush();
	}
	@Inject
	SmsService smsService;
	@Inject
	DeterminedRecordService recordService;
	
	public String audit() {
		User auditedUser = userService.getLoginedUser();
		getInstance().setAuditDate(new Date());
		getInstance().setAuditUser(auditedUser);
		getInstance().setAuditted(true);
		//当 保留待定名单审核通过时，需要将新设置的截至日期 给截至日期字段赋值
		RecordStatus recordStatus = getInstance().getRecordStatus();
		if(recordStatus.equals(RecordStatus.Keeped)) {
			
			getInstance().setEndDate(getInstance().getNewEndDate());
		}
		DeterminedRecordAuditHistory his = generateHistory(true, "");
		getInstance().getAuditHistory().add(his);
		String result = super.saveAndFlush();
		//发送通知信息
		if(result.equals(RESULT_SAVED) && recordStatus.equals(RecordStatus.Keeped)) {
			smsService.determinedRecordSuccessNotification(getInstance().getSupplier());
		}
		return result;		
	}
	@Inject
	RejectRecordHome rejectRecordHome;
	public String reject() {
		User auditedUser = userService.getLoginedUser();
		getInstance().setAuditDate(new Date());
		getInstance().setAuditUser(auditedUser);
		getInstance().setAuditted(false);	
		RejectRecord rejectRecord = rejectRecordHome.getInstance();
		getInstance().setRejectRecord(rejectRecord);
		
		DeterminedRecordAuditHistory his = generateHistory(false, rejectRecord.getReason());
		getInstance().getAuditHistory().add(his);
		String result = super.saveAndFlush();
		return result;		
	}
	
	public DeterminedRecordAuditHistory generateHistory(boolean auditted,String reason) {
		DeterminedRecordAuditHistory his = new DeterminedRecordAuditHistory();
		his.setAuditDate(new Date());
		his.setAuditedUserCnname(userService.getLoginedUser().getCnname());
		his.setSubmitUserCnname(getInstance().getSubmitUser().getCnname());
		his.setRecordStatus(getInstance().getRecordStatus());
		his.setAuditted(auditted);
		his.setReason(reason);
		return his;
	}
	@Inject
	AttachmentUtils attachmentUtils;
	public void uploadAttachment(FileUploadEvent event) {
		if (event.getFile() == null)
			return ;
		
		Attachment attr = attachmentUtils.getAttachmentFromUpload(event.getFile());
		
		getInstance().getAttachments().add(attr);
		FacesMessage message = new FacesMessage(event.getFile().getFileName() + " 上传成功。");
        FacesContext.getCurrentInstance().addMessage(null, message);
	}
	
	
	public String submitToAuditing(boolean isKeep) {
		if(getInstance().getAttachments().size() == 0) {
			FacesContext.getCurrentInstance().addMessage("attachments",new FacesMessage(FacesMessage.SEVERITY_ERROR, "请至少上传一个整改情况材料！", ""));
			return null;
		}
		if(isKeep) getInstance().setRecordStatus(RecordStatus.Keeped);
		else getInstance().setRecordStatus(RecordStatus.Deleted);
		getInstance().setAuditted(null);
		getInstance().setRejectRecord(null);
		getInstance().setSubmitUser(userService.getLoginedUser());
		getInstance().setSubmitDate(new Date());
		return super.saveAndFlush();
	}
	
	public void removeAttachment(Attachment attachment) {
		getInstance().getAttachments().remove(attachment);
	}
	
	
	
	@Override
	public String delete() {
		getInstance().setDelFlag(DataEntity.DEL_FLAG_NORMAL);
		return super.saveAndFlush();
	}
	
	
	
	
	
}
